﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Helpers;
using Lead2012.Business;

namespace Lead2012.Repository
{
    public class UserManagementRepository
    {
        // Create an instance of entities framework
        Lead2012Entities entities = new Lead2012Entities();

        public void GetModel(UserViewModel model, string keywords, string sortBy, string sortOrder, int pageIndex, int pageSize)
        {
            int counts = 0;
            model.ItemList = GetAllUser(keywords, sortBy, sortOrder, pageIndex, pageSize, ref counts);            

            // Calculate paging
            if (counts > 0)
            {
                model.TotalPage = (counts % pageSize == 0) ? counts / pageSize : (counts / pageSize + 1);
                model.TotalRecord = counts;
                model.PageIndex = pageIndex;
                model.PageSize = pageSize;
            }
        }

        public void GetModel(UserViewModel model, int id)
        {
            var user = new UserRepository().GetSingle(id);
            UserInfo userInfo = new UserInfo();

            if (user != null)
            {
                userInfo.FirstName = user.FirstName;
                userInfo.LastName = user.LastName;
                userInfo.Email = user.Email;
                userInfo.IsLockedOut = Convert.ToBoolean(user.IsLockedOut);

                model.UserInfo = userInfo;
            }
        }
        
        #region #Common
        public List<User> GetAllUser(string keywords, string sortBy, string sortOrder, int page, int take, ref int counts)
        {
            List<User> list = new List<User>();
           
            var items = new UserRepository().GetAll();
                        
            // Add to list
            foreach (var item in items)
            {
                User model = new User();
                model.ID = item.ID;
                model.Fullname = item.Fullname;
                model.FirstName = item.FirstName;
                model.LastName = item.LastName;
                model.Username = item.Username;
                model.Active = item.Active;
                model.IsApproved = item.IsApproved;
                model.Comment = new EFRoleProvider().GetRolesForUser(model.Username)[0];
                list.Add(model);
            }
           
            if (!string.IsNullOrEmpty(keywords))
            {
                list = list.Where(c => c.Username.ToLower().Contains(keywords.ToLower())).ToList();
            }

            counts = list.Count;

            
            // Add item to list
            switch (sortBy)
            {
                case "UserName":
                    {
                        if (sortOrder == "asc")
                          list =  list.OrderBy(c => c.Username).Skip((page - 1) * take).Take(take).ToList();
                        else
                          list =  list.OrderByDescending(c => c.Username).Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
                    default:
                    {
                        if (sortOrder == "desc")
                            list = list.OrderBy(c => c.ID).Skip((page - 1) * take).Take(take).ToList();
                        else
                            list = list.OrderByDescending(c => c.ID).Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
            }
            
            return list;
        }
        #endregion
    }
}